<?php
namespace app\index\controller;

use think\Request;
use think\Config;

class Address extends Common{
	
	//代理自己的地址列表
	public function index(){
		$req = Request::instance();
		$addressList = $this->getAddressList();
		
		if($req->isAjax()){
			ajax('SUCCESS',true,$addressList);
		}else{
			$onlyselect = Request::instance()->param('onlyselect',0,'intval');
			return view('',['list'=>$addressList,'onlyselect'=>$onlyselect]);
		}
	}
	
	//保存地址信息
	public function save(){
		if(Request::instance()->isPost()){
			$tsid = Request::instance()->param('tsid',0,'intval');
			$action = Request::instance()->param('action');
			$param = [
				'action' => $action,
				'address' => json_encode([
					'tsid'   => $tsid,
					'agentid' => AGENT_ID,
					'consignee' => Request::instance()->param('consignee'), // shou huo ren
					'phonenum' => Request::instance()->param('phonenum'),
					'province' => Request::instance()->param('province'),
					'city' => Request::instance()->param('city'),
					'region' => Request::instance()->param('region'),
					'address' => Request::instance()->param('address'),
					'isdefault' => 0,
				])
			];
			$response = \ServerHelper::post('address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax($action == 'add' ? '新增失败' : '保存失败');
			}else{
				//重新拉取所有地址列表
				$addressList = $this->getAddressList();
				ajax($action == 'add' ? '新增成功' : '保存成功',true,$addressList);
			}
		}
		return view();
	}
	
	//获取当前代理收货地址列表
	private function getAddressList(){
		$param = [
			'action' => 'list',
			'agentid' => AGENT_ID
		];
		$response = \ServerHelper::post('address',$param);
		$list = $this->J($response);
		if(!isset($list['data'])){
			$list['data'] = [];
		}
		return $list['data'];
	}
  
	public function edit(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		if(Request::instance()->isPost()){
			
			$action = Request::instance()->param('action');
			$param = [
				'action' => 'edit',
				'address' => json_encode([
					'tsid'   => $tsid,
					'agentid' => AGENT_ID,
					'consignee' => Request::instance()->param('consignee'), // shou huo ren
					'phonenum' => Request::instance()->param('phonenum'),
					'province' => Request::instance()->param('province'),
					'city' => Request::instance()->param('city'),
					'region' => Request::instance()->param('region'),
					'address' => Request::instance()->param('address'),
					'isdefault' => 0,
				])
			];
			
			$response = \ServerHelper::post('address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax('保存失败');
			}else{
				ajax('保存成功');
			}
		}else{
			$param = [
				'action' => 'list',
				'agentid' => AGENT_ID,
				'tsid' =>(int)$tsid
			];

			$response = \ServerHelper::post('address',$param);
			$data = $this->J($response);
			if(empty($data['flag'])){
				ajax('获取失败');
			}
			ajax("SUCCESS",true,$data['data']);
		}
	}
  
	public function delete(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		$param = [
			'action' => 'delete',
			'agentid' => AGENT_ID,
			'tsid' =>(int)$tsid
		];
		$response = \ServerHelper::post('address',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('删除失败');
		}
		ajax('删除成功',true);
    
	}
	public function setdefault(){
		$tsid = Request::instance()->param('tsid');
		if(!\RegexHelper::is_id($tsid)){
			$this->error('参数错误');
		}
		$param = [
			'action' => 'setdefault',
			'agentid' => AGENT_ID,
			'tsid' =>(int)$tsid
		];
		$response = \ServerHelper::post('address',$param);
		$data = $this->J($response);
		if(empty($data['flag'])){
			ajax('设置失败');
		}
		ajax('设置成功',true);
	}
	
	//搜集无法定义到轮子的地址
	//主要来源于地址智能识别后
	public function collect(){
		$data = Request::instance()->param('data');
		if($data == ''){
			ajax('不能为空');
		}
		__log($data,'addres_collect');
		ajax('收集成功',true);
	}
	
	//识别地址
	public function shibie(){
		$content = Request::instance()->param('content');
		if($content == '') ajax('不能为空');
		$api_key = config::get('nawen.BAIDU_ADDRESS_APIKEY');
		$api_secret = config::get('nawen.BAIDU_ADDRESS_SECRETKEY');
		$hp = new \BaiduAddress\BaiduAddress($api_key,$api_secret);
		if(!$hp->getToken())
			ajax('配置错误');
		$result = $hp->address($content);
		if(isset($result['province']) && isset($result['city'])){
			ajax('识别成功',true,[
				'province' => $result['province'],
				'city' => $result['city'],
				'county' => $result['county'],
				'address' => $result['town'] . $result['detail'],
				'phone' => $result['phonenum'],
				'name' => $result['person'],
			]);
		}
		ajax('识别失败');
	}
	
}
